﻿using System;
using System.Collections.Generic;
using System.Text;
using TVT.Common.Helpers;

namespace Common.Helpers
{
    public class DateHelper
    {
        public static bool IsWorkingDay(DateTime date)
        {
            switch (date.DayOfWeek)
            {
                case DayOfWeek.Sunday:
                case DayOfWeek.Saturday:
                    return false;
                    break;
                default:
                    return true;
                    break;
            }
        }

        public static DateTime GetLastWorkingDayFromDate(DateTime date)
        {
            return GetWorkingDate(date, -1);
        }

        public static DateTime GetNextWorkingDayFromDate(DateTime date)
        {
            return GetWorkingDate(date, 1);
        }

        private static DateTime GetWorkingDate(DateTime date, int increase)
        {
            DateTime returnDate = date;
            bool isWorkingDate = false;

            while (!isWorkingDate)
            {
                switch (returnDate.DayOfWeek)
                {
                    case DayOfWeek.Sunday:
                    case DayOfWeek.Saturday:
                        returnDate = returnDate.AddDays(increase);
                        break;
                    default:
                        isWorkingDate = true;
                        break;
                }
            }
            return returnDate;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns>YYYY[separator]MM[separator]DD</returns>
        public static String GetSeparetedStringDate(DateTime date, String separator)
        {
            String year = date.Year.ToString();
            String month = date.Month.ToString();
            String day = date.Day.ToString();

            if (month.Length < 2)
                month = "0" + month;
            if (day.Length < 2)
                day = "0" + day;

            return year + separator + month + separator + day;
        }

        /// <summary>
        /// YYYY[separator]MM[separator]DD
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static DateTime GetDateFromSeparetedString(String date, String separator)
        {
            int year = int.Parse(date.Substring(0, 4));
            int month = int.Parse(date.Substring(4 + separator.Length, 2));
            int day = int.Parse(date.Substring(6 + 2 * separator.Length));
            return new DateTime(year, month, day);

        }
    }
}
